﻿Imports System.Data
Imports VSModel
Imports Cls_Common

Partial Class Control_Raovat_TienNghi
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            If Not Request.QueryString("RaovatId") Is Nothing Then
                hidID.Value = Request.QueryString("RaovatId")
                ShowData()
            End If
        End If


    End Sub
    Protected Sub ShowData()
        Using data As New VSEntities
            Dim p As TienNghi = (From q In data.TienNghis Where q.RoaVatId = hidRaoVatId.Value Select q).FirstOrDefault
            If Not p Is Nothing Then
                rptThietBiDinhVi.Checked = IIf(IsNothing(p.ThietBiDinhVi) = True, "", p.ThietBiDinhVi)
                rptCuaSoNoc.Checked = IIf(IsNothing(p.CuaSoNoc) = True, "", p.CuaSoNoc)
                rptKinhChinhDien.Checked = IIf(IsNothing(p.KinhChinhDien) = True, "", p.KinhChinhDien)
                rptTayLaiTroLuc.Checked = IIf(IsNothing(p.TayLaiTroLuc) = True, "", p.TayLaiTroLuc)
                txtGheChatLieu.Text = IIf(IsNothing(p.GheChatLieuTienNghi) = True, "", p.GheChatLieuTienNghi)
                rptDieuHoaTruoc.Checked = IIf(IsNothing(p.DieuHoaTruoc) = True, "", p.DieuHoaTruoc)
                rptDieuHoaSau.Checked = IIf(IsNothing(p.DieuHoaSau) = True, "", p.DieuHoaSau)
                rptHoTroXeTuDong.Checked = IIf(IsNothing(p.HoTroXeTuDong) = True, "", p.HoTroXeTuDong)
                rptSaykinhsau.Checked = IIf(IsNothing(p.SayKinhSau) = True, "", p.SayKinhSau)
                rptQuatKinhSau.Checked = IIf(IsNothing(p.QuatKinhPhiaSau) = True, "", p.QuatKinhPhiaSau)
                rptKinhMau.Checked = IIf(IsNothing(p.KinhMau) = True, "", p.KinhMau)
                rptLCD.Checked = IIf(IsNothing(p.ManHinhLCD) = True, "", p.ManHinhLCD)
                rptGiaiTri.Checked = IIf(IsNothing(p.ThietBiGiaiTri) = True, "", p.ThietBiGiaiTri)
                txtDescription.Text = IIf(IsNothing(p.ThietBiKhac) = True, "", p.ThietBiKhac)

            End If
        End Using
    End Sub
#End Region
#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            If hidRaoVatId.Value <> "0" Then
                Dim p As TienNghi = (From q In data.TienNghis Where q.RoaVatId = hidRaoVatId.Value Select q).FirstOrDefault
                Try
                    p.ThietBiDinhVi = rptThietBiDinhVi.Checked
                    p.CuaSoNoc = rptCuaSoNoc.Checked
                    p.KinhChinhDien = rptKinhChinhDien.Checked
                    p.TayLaiTroLuc = rptTayLaiTroLuc.Checked
                    p.GheChatLieuTienNghi = txtGheChatLieu.Text.Trim()
                    p.DieuHoaTruoc = rptDieuHoaTruoc.Checked
                    p.DieuHoaSau = rptDieuHoaSau.Checked
                    p.HoTroXeTuDong = rptHoTroXeTuDong.Checked
                    p.SayKinhSau = rptSaykinhsau.Checked
                    p.QuatKinhPhiaSau = rptQuatKinhSau.Checked
                    p.KinhMau = rptKinhMau.Checked
                    p.ManHinhLCD = rptLCD.Checked
                    p.ThietBiGiaiTri = rptGiaiTri.Checked
                    p.ThietBiKhac = txtDescription.Text.Trim
                    p.RoaVatId = hidRaoVatId.Value
                    data.SaveChanges()
                    'Insert_App_Log("Insert Advertise:" & txtTitle.Text.Trim & "", Function_Name.Advertisement, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                    Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Raovat/Step4.aspx?RaovatId=" + hidRaoVatId.Value + "';", Me.Page, True)
                Catch ex As Exception
                    log4net.Config.XmlConfigurator.Configure()
                    log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                    Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
                End Try
            Else
                Dim p As New VSModel.TienNghi
                Try
                    p.ThietBiDinhVi = rptThietBiDinhVi.Checked
                    p.CuaSoNoc = rptCuaSoNoc.Checked
                    p.KinhChinhDien = rptKinhChinhDien.Checked
                    p.TayLaiTroLuc = rptTayLaiTroLuc.Checked
                    p.GheChatLieuTienNghi = txtGheChatLieu.Text.Trim()
                    p.DieuHoaTruoc = rptDieuHoaTruoc.Checked
                    p.DieuHoaSau = rptDieuHoaSau.Checked
                    p.HoTroXeTuDong = rptHoTroXeTuDong.Checked
                    p.SayKinhSau = rptSaykinhsau.Checked
                    p.QuatKinhPhiaSau = rptQuatKinhSau.Checked
                    p.KinhMau = rptKinhMau.Checked
                    p.ManHinhLCD = rptLCD.Checked
                    p.ThietBiGiaiTri = rptGiaiTri.Checked
                    p.ThietBiKhac = txtDescription.Text.Trim
                    p.RoaVatId = hidRaoVatId.Value
                    data.TienNghis.AddObject(p)
                    data.SaveChanges()

                    'Insert_App_Log("Insert Advertise:" & txtTitle.Text.Trim & "", Function_Name.Advertisement, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                    Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Raovat/Step4.aspx?RaovatId=" + hidRaoVatId.Value + "';", Me.Page, True)
                Catch ex As Exception
                    log4net.Config.XmlConfigurator.Configure()
                    log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                    Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
                End Try
            End If


        End Using
    End Sub
    Protected Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region
End Class
